﻿#include "QtWidgetsApplication.h"

static inline void MoveSlider(QSlider* pSlider, QSpinBox* pSpinBox, QPushButton* Sliderbutton) {
    pSlider->setGeometry(10000, 10000, 100, 100);
    pSpinBox->setGeometry(10000, 10000, 100, 100);
    Sliderbutton->setGeometry(10000, 10000, 100, 100);
}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
    this->resize(1920/2, 1080/2);
    this->setWindowTitle("Notepad");
    this->setWindowIcon(QIcon(":/images/file/notepad.png"));
    imageLabel = new ImageLabel();

    int nMin = -100;
    int nMax = 100;
    int nSingleStep = 1;

    pSpinBox = new QSpinBox(this);
    pSpinBox->setMinimum(nMin);  // 最小值
    pSpinBox->setMaximum(nMax);  // 最大值
    pSpinBox->setSingleStep(nSingleStep);  // 步长

    pSlider = new QSlider(this);
    pSlider->setOrientation(Qt::Horizontal);  // 水平方向
    pSlider->setMinimum(nMin);  // 最小值
    pSlider->setMaximum(nMax);  // 最大值
    pSlider->setSingleStep(nSingleStep);  // 步长

    Sliderbutton = new QPushButton(this);
    Sliderbutton->setText("save");
    MoveSlider(pSlider, pSpinBox, Sliderbutton);

        // 连接信号槽（相互改变）
    connect(Sliderbutton, SIGNAL(clicked()), this, SLOT(savechange()));
    connect(pSpinBox, SIGNAL(valueChanged(int)), pSlider, SLOT(setValue(int)));
    connect(pSlider, SIGNAL(valueChanged(int)), pSpinBox, SLOT(setValue(int)));
    connect(pSlider, SIGNAL(valueChanged(int)), this, SLOT(updatesaturation(int)));
    connect(pSpinBox, SIGNAL(valueChanged(int)), this, SLOT(updatesaturation(int)));

    pSpinBox->setValue(0);

    newAct = new QAction(QIcon(":/images/file/new_file.png"), tr("new"), this);
    newAct->setShortcut(tr("Ctrl+N"));       					
    newAct->setStatusTip(tr("new file"));     					 
    openAct = new QAction(QIcon(":/images/file/open_file.png"), tr("open"), this);
    openAct->setShortcut(tr("Ctrl+O"));
    openAct->setStatusTip(tr("open file"));
    saveAct = new QAction(QIcon(":/images/file/save_file.png"), tr("save"), this);
    saveAct->setShortcut(tr("Ctrl+S"));
    saveAct->setStatusTip(tr("save file"));
    saveasAct = new QAction(QIcon(":/images/file/save_as.png"), tr("save as"), this);
    saveasAct->setShortcut(tr("Ctrl+Shift+S"));
    saveasAct->setStatusTip(tr("save as file"));

    pencilAct = new QAction(QIcon(":/images/file/save_as.png"), tr("pen"), this);
    pencilAct->setStatusTip(tr("save as file"));

    vacuityAct = new QAction(QIcon(":/images/file/save_as.png"), tr("vacuity"), this);
    vacuityAct->setStatusTip(tr("save as file"));

    sharpeningAct = new QAction(QIcon(":/images/file/save_as.png"), tr("sharpening"), this);
    sharpeningAct->setStatusTip(tr("save as file"));

    edgeAct = new QAction(QIcon(":/images/file/save_as.png"), tr("canny"), this);
    edgeAct->setStatusTip(tr("save as file"));

    moveAct = new QAction(QIcon(":/images/file/save_as.png"), tr("move"), this);
    moveAct->setStatusTip(tr("save as file"));

    saturationAct = new QAction(QIcon(":/images/file/save_as.png"), tr("saturation"), this);
    saturationAct->setStatusTip(tr("save as file"));

    clear = new QAction(QIcon(":/images/file/save_as.png"), tr("clear"), this);
    clear->setStatusTip(tr("clear"));

    drawrect = new QAction(QIcon(":/images/file/save_as.png"), tr("draw rect"), this);
    drawrect->setStatusTip(tr("draw rect"));

    drawcyc = new QAction(QIcon(":/images/file/save_as.png"), tr("draw cycle"), this);
    drawcyc->setStatusTip(tr("draw cycle"));

    connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
    connect(openAct, SIGNAL(triggered()), this, SLOT(openFile()));
    connect(saveAct, SIGNAL(triggered()), this, SLOT(saveFile()));
    connect(saveasAct, SIGNAL(triggered()), this, SLOT(saveasFile()));

    connect(pencilAct, SIGNAL(triggered()), this, SLOT(usepencil()));
    connect(vacuityAct, SIGNAL(triggered()), this, SLOT(usevacuity()));
    connect(sharpeningAct, SIGNAL(triggered()), this, SLOT(usesharpening()));
    connect(edgeAct, SIGNAL(triggered()), this, SLOT(useedge()));
    connect(moveAct, SIGNAL(triggered()), this, SLOT(usemove()));
    connect(saturationAct, SIGNAL(triggered()), this, SLOT(usesaturation()));
    connect(clear, SIGNAL(triggered()), this, SLOT(useclear()));
    connect(drawrect, SIGNAL(triggered()), this, SLOT(usedrawrect()));
    connect(drawcyc, SIGNAL(triggered()), this, SLOT(usedrawcyc()));

    fileMenu = menuBar()->addMenu(tr("file"));
    fileMenu->addAction(newAct);
    fileMenu->addAction(openAct);
    fileMenu->addAction(saveAct);
    fileMenu->addAction(saveAct);
    fileMenu->addSeparator();

    editMenu = menuBar()->addMenu(tr("edit"));
    editMenu->addAction(moveAct);
    editMenu->addAction(pencilAct);
    editMenu->addAction(vacuityAct);
    editMenu->addAction(sharpeningAct);
    editMenu->addAction(edgeAct);
    editMenu->addAction(saturationAct);
    editMenu->addAction(clear);
    editMenu->addAction(drawrect);
    editMenu->addAction(drawcyc);
    editMenu->addSeparator();

    imageLabel->setParent(this);
    imageLabel->setGeometry(0, 25, this->size().width(), this->size().height());
    imageLabel->setPainter();
    imageLabel->initCanvas();
}


void MainWindow::newFile()
{
    currentFile.clear();
    imageLabel->setImage(QImage(imageLabel->size(), QImage::Format_RGB888));
}

void MainWindow::openFile()
{
    QString fileName = QFileDialog::getOpenFileName(this, "Open file");
    imageLabel->setImage(fileName);
}

void MainWindow::saveFile()
{
    QString fileName;
    if (currentFile.isEmpty()) {
        fileName = QFileDialog::getSaveFileName(this, "Save");
        currentFile = fileName;
    }
    else {
        fileName = currentFile;
    }
    QFile file(fileName);
    if (!file.open(QIODevice::WriteOnly | QFile::Text)) {
        QMessageBox::warning(this, "WARNING!", "can open file: " + file.errorString());
        return;
    }
    setWindowTitle(fileName);
    imageLabel->saveImage(fileName);
    file.close();
}

void MainWindow::saveasFile()
{
    QString fileName = QFileDialog::getSaveFileName(this, "Save as");
    QFile file(fileName);

    if (!file.open(QFile::WriteOnly | QFile::Text)) {
        QMessageBox::warning(this, "WARNING!", "can open file: " + file.errorString());
        return;
    }
    currentFile = fileName;
    setWindowTitle(fileName);
    imageLabel->saveImage(fileName);
    file.close();
}

void MainWindow::usepencil() {
    MoveSlider(pSlider, pSpinBox, Sliderbutton);
    imageLabel->setType(PEN);
}

void MainWindow::usevacuity() {
    MoveSlider(pSlider, pSpinBox, Sliderbutton);
    QImage m = blur(imageLabel->getImage());
    imageLabel->setImage(m);
    imageLabel->updateImage();
    imageLabel->setType(SH);
}

void MainWindow::usesharpening() {
    MoveSlider(pSlider, pSpinBox, Sliderbutton);
    QImage m = sharpen(imageLabel->getImage());
    imageLabel->setImage(m);
    imageLabel->updateImage();
    imageLabel->setType(SH);
}

void MainWindow::useedge() {
    MoveSlider(pSlider, pSpinBox, Sliderbutton);
}

void MainWindow::usemove() {
    imageLabel->setType(MOVE);
}

void MainWindow::usesaturation() {
    
    pSlider->setGeometry(300, 0, 150, 20);
    pSpinBox->setGeometry(250, 0, 50, 20);
    Sliderbutton->setGeometry(450, 0, 40, 20);
}

void MainWindow::updatesaturation(int v) {
    QImage m  = saturation(v, imageLabel->getImage());
    imageLabel->paintImage(m);
    imageLabel->updateCanvas();
}

void MainWindow::savechange() {
    QImage m = saturation(pSlider->value(), imageLabel->getImage());
    imageLabel->setImage(m);
}

void MainWindow::useclear() {
    imageLabel->clear();
}

void MainWindow::usedrawrect() {
    imageLabel->setType(DRAW_RECT);
}
void MainWindow::usedrawcyc() {
    imageLabel->setType(DRAW_CYC);
}

